#include "chooselevelscene.h"
#include "mypushbutton.h"

#define cout qDebug() << "[" << __FILE__ << ":" << __LINE__ <<"]"

ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    // 配置关卡场景
    this->setFixedSize(350, 588);
    this->setWindowTitle("Choose Level");
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));

    // 选关卡音效资源
    QSound *chooseSound = new QSound(":/res/TapButtonSound.wav", this);
    QSound *backSound = new QSound(":/res/BackButtonSound.wav", this);

    // 创建菜单栏
    QMenuBar *mBar = menuBar();
    setMenuBar(mBar);

    QMenu *startMenu = mBar->addMenu("Start"); // 创建开始菜单
    QAction *quitAction = startMenu->addAction("quit");

    connect(quitAction, &QAction::triggered, [=]() {
        this->close();
    });

    // 返回按钮设计
    MyPushButton *backBtn = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width() - backBtn->width(), this->height() - backBtn->height());

    // 点击backBtn返回上一个界面
    connect(backBtn, &MyPushButton::clicked, [=]() {
        backSound->play();

        QTimer::singleShot(500, this, [=]() {
             this->hide();

             emit showMainnSceneSignal();

        });
    });

    // 创建选择关卡的场景
    for(int i = 0; i < 20; ++i) {
        MyPushButton *menuBtn = new MyPushButton(":/res/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(45 + i%4 * 70, 150 + i/4 * 70);

        // 监听每个按钮的点击事件
        connect(menuBtn, &MyPushButton::clicked, [=]() {
            chooseSound->play();
            cout << i+1 ;

            // 进入游戏场景
            this->hide();
            play = new PlayScene(i+1);

            // 设置游戏场景初始位置
            play->setGeometry(this->geometry());
            play->show();


            connect(play, &PlayScene::backToLevelSceneSignal, [=]() {
                this->setGeometry(play->geometry());
               this->show();
               delete play;
               play = NULL;
            });


        });

        // 在按钮上写上数字 这里有两种写法（父对象设置的不同，造成影响不同）
        // （1）设置父对象为按钮
//        QLabel *label = new QLabel(menuBtn);

//        label->setFixedSize(menuBtn->width(), menuBtn->height());
//        label->setText(QString::number(i+1));
//        label->setAlignment(Qt::AlignCenter | Qt::AlignVCenter);

        // （2）设置父对象为这个窗口
        QLabel *label = new QLabel(this);
        label->setFixedSize(menuBtn->width(), menuBtn->height());
        label->move(45 + i%4 * 70, 150 + i/4 * 70);
        label->setText(QString::number(i+1));
        label->setAlignment(Qt::AlignCenter | Qt::AlignVCenter);
        // 鼠标击穿设置
        label->setAttribute(Qt::WA_TransparentForMouseEvents);

    }

}

void ChooseLevelScene::paintEvent(QPaintEvent *event)
{
    QPainter paint(this);

    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    paint.drawPixmap(0, 0, this->width(), this->height(), pix);

    pix.load(":/res/Title.png");
    paint.drawPixmap(25, 30, pix);

}





